﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace RG.Common
{
    public static class ConfigHelper
    {
        public static T tryReadConfig<T>(string path) where T : class
        {
            try
            {
                using (var configFile = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    return (T)(new XmlSerializer(typeof(T))).Deserialize(configFile);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static T readConfig<T>(string AppName) where T : class
        {
            return tryReadConfig<T>(Environment.GetEnvironmentVariable("WinDir") + "\\" + AppName + ".config") ??
                   tryReadConfig<T>(AppDomain.CurrentDomain.BaseDirectory + "\\" + AppName + ".config");
        }
    }
}
